package cn.itcast.up32.up_web.controller;

import cn.itcast.up32.up_web.bean.HttpResult;
import cn.itcast.up32.up_web.bean.dto.TagDto;
import cn.itcast.up32.up_web.bean.dto.TagModelDto;
import cn.itcast.up32.up_web.service.TagService;
import cn.itcast.up32.up_web.util.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//@RequestMapping("tags")
public class TagController {
    //接收123级标签的创建请求
    @Autowired
    private TagService tagService;

    @PutMapping("tags/relation")
    @ResponseBody         //json转换对象
    public void putTags(@RequestBody List<TagDto> tags) {
        tagService.save(tags);
    }

    /**
     * 按照pid进行标签查询
     *
     * @param pid
     */
    @GetMapping("tags")
    public HttpResult<List<TagDto>> getTags(Long pid, Integer level) {
        List<TagDto> list = null;
        if (pid == null) {
            //如果pid为null,按照等级进行查询
            list = tagService.findByLevel(level);
        }
        if (level == null) {
            //如果等级为null,按照pid进行查询
            list = tagService.findByPid(pid);
        }
        return new HttpResult<List<TagDto>>(HttpStatus.SUCCESS,"查询成功",list);
    }
    @PutMapping("tags/model")
    public HttpResult putTagModel(@RequestBody TagModelDto tagModelDto){
        System.out.println(tagModelDto);
        //调用service开始保存标签和模型
        tagService.addTagModel(tagModelDto.getTag(), tagModelDto.getModel());
        return new HttpResult(HttpStatus.SUCCESS, "成功", null);
    }
}
